import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * @className: ClientThread
 * @description: 客户端线程,处理
 * @author: WangXiaosong
 * @date: 2022/6/8
 **/
public class ClientThread extends Thread {
    private Socket client = null;


    public ClientThread(Socket socket) {
        this.client = socket;
    }

    @Override
    public void run() {
        try {
            //获取用户名,密码信息,并发送给服务端
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));//从控制台获取信息
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
            while (true) {
                String info = getInfo(bufferedReader);//获取用户名,密码信息
                bufferedWriter.write(info);
                bufferedWriter.write("\n");//表示一条信息结束了，服务端通过
                bufferedWriter.flush();

                //读取服务器返回的消息
                BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
                String mess = br.readLine();
                System.out.println("【收到服务器信息】:" + mess);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 从控制台获取用户名,密码信息
     * @param bufferedReader 输入流
     * @return 用户名,密码字符串.空格隔开
     * @throws IOException
     */
    private static String getInfo(BufferedReader bufferedReader) throws IOException {
        System.out.println("user:");
        String user = bufferedReader.readLine();
        System.out.println("pwd:");
        String pwd = bufferedReader.readLine();
        return user + " " + pwd;
    }
}
